<?php


namespace App\Http\Controllers\Api\Hotel;


use App\Facades\Services\StoreGoodsService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class StoreGoodsController extends Controller
{

    /**
     * 获取所有商品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAllStoreGoods(Request $request)
    {
       return formatJsonResponse(StoreGoodsService::getStoreGoods($request->all()));
    }

    /**
     * 获取商品详细信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidateParameterException
     */
    public function getStoreGoods(Request $request)
    {
        $rules = [
            'id' => 'required'
        ];

        $this->validate($request, $rules);

        return formatJsonResponse(StoreGoodsService::getStoreGoods($request->all()));
    }


    /**
     * 增加商品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidateParameterException
     */
    public function createStoreGoods(Request $request)
    {

        $rules = [
            'goods_name' => 'bail|required|string',
            'goods_img' => 'bail|required|image|max:5120|mimes:jpg,jpeg,png,bmp,gif,svg',
            'goods_spec' => 'bail|required|string',
            'goods_number' => 'bail|required|string',
            'goods_price' => 'bail|required',
            'goods_type' => 'bail|required|integer',
            'level_id' => 'bail|required|exists:store_levels,id',
            'is_hot' => 'bail|filled|boolean'
        ];

        $this->validate($request, $rules);

        StoreGoodsService::createStoreGoods($request);

        return formatJsonResponse();

    }


    /**
     * 更新商品信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidateParameterException
     */
    public function updateStoreGoods(Request $request)
    {
        $rules = [
            'id' => 'required',
            'goods_name' => 'bail|filled|string',
            'goods_img' => 'bail|filled|image|max:5120|mimes:jpg,jpeg,png,bmp,gif,svg',
            'goods_spec' => 'bail|filled|string',
            'goods_number' => 'bail|filled|string',
            'goods_price' => 'bail|filled',
            'goods_type' => 'bail|filled|integer',
            'level_id' => 'bail|filled|exists:store_levels,id',
            'hotel_id' => 'bail|filled|exists:hotels,id',
            'is_hot' => 'bail|filled|boolean'
        ];

        $this->validate($request, $rules);

        StoreGoodsService::updateStoreGoods($request);

        return formatJsonResponse();
    }

    /**
     * 删除商品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidateParameterException
     */
    public function deleteStoreGoods(Request $request)
    {
        $rules = [
            'id' => 'required'
        ];

        $this->validate($request, $rules);

        StoreGoodsService::deleteStoreGoods($request->all());

        return formatJsonResponse();

    }
}